// orderconfirm.js
var app = getApp();

function countdown(that) {
  var second = that.data.second;
  var minute = that.data.minute;
  if (minute <= 0 && second <= 0) {
    that.setData({
      minute: 0
    });
    return;
  }
  var time = setTimeout(function() {
    that.setData({
      second: second - 1
    });
    if (second <= 0) {
      that.setData({
        minute: minute - 1,
        second: 59
      });
    }
    countdown(that);
  }, 1000)
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    second: 60,
    minute: 4,
    disabled: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    app.showLoadings();
    this.setData({
      issharing: options.issharing
    })
    var that = this;
    try {
      if (options.succeed == 'true') {
        wx.getStorage({
          key: 'newOrderMess',
          success: function(res) {
            that.setData({
              mes: res.data.data,
              issharing: options.issharing,
              // messages: 
            });
          },
        });
      } else {
        wx.getStorage({
          key: 'orderMess',
          success: function(res) {
            that.setData({
              mes: res.data,
              issharing: options.issharing
            });
          },
        });
      }
      app.hideLoadings();
    } catch (err) {}

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    countdown(this);
  },
  openToast: function() {
    wx.showToast({
      title: '支付完成',
      icon: 'success',
      duration: 600
    });
  },
  openToasts: function() {
    wx.showToast({
      title: '支付失败',
      icon: 'success',
      duration: 600
    });
  },
  wxpay: function(e) {
    this.setData({
      disabled: true
    });
    var that = this,
      OrderCode = e.currentTarget.dataset.indent;
    // 调用后台支付接口，由后台服务器请求微信的预支付接口
    wx.request({
      url: app.serviceurl + 'CarRental/CarRentalPayOrder',
      header: {
        Authorization: "Bearer " + app.accesstoken
      },
      data: {
        OrderCode: OrderCode
      },
      dataType: 'json',
      success: function(res) {
        // 后台调用微信预支付接口返回的数据
        if (!res.data.Result) {
          that.openToasts();
          return
        };
        if (res.data.Result) {
          // 微信支付接口
          wx.requestPayment({
            // 接口参数
            timeStamp: res.data.timeStamp,
            nonceStr: res.data.nonceStr,
            package: res.data.package,
            signType: res.data.signType,
            paySign: res.data.paySign,
            // 返回的数据
            success: function(resr) {
              // 支付成功
              if (resr.errMsg == "requestPayment:ok") {
                // app.carHint = false;
                that.openToast();
                var orderId = e.currentTarget.dataset.indent;
                // 发送一个通知给后台，告诉用户已经支付完成，让后台检测一下订单状态是否正确更改，根据情况向用户发送即时通知。
                wx.request({
                  url: app.serviceurl + 'CarRental/CarRentalPayFinishedTemplate',
                  header: {
                    Authorization: "Bearer " + app.accesstoken
                  },
                  data: {
                    orderSn: OrderCode,
                    package: res.data.package
                  },
                  success: function(res) {
                    //不需要处理
                    wx.redirectTo({
                      url: '../indent/indent?ordercode=' + OrderCode + '&issharing=' + that.data.issharing
                    })
                  }
                });
              }
            }
          })
        } else {
          wx.showToast({
            title: '调起支付失败',
          })
        }
      },
      fail: function(res) {
        // console.log(res)
      },
      complete: function() {
        var time = setTimeout(function() {
          that.setData({
            disabled: false,
          });
        }, 1000)
      }
    })
  },

  skip: function() {
    if (this.data.mes.Message == '目前您还有未处理订单，请先到未完成订单进行处理！') {
      wx.redirectTo({
        url: '../indent/indent?issharing=' + this.data.issharing
      })
    } else {
      wx.navigateBack({})
    }

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    // clearTimeout(this.data.time)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    // 清除定时器
    // clearTimeout(this.data.time)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  phoneservice: function(e) {
    wx.makePhoneCall({
      phoneNumber: "96606"
    });
  },
})